// https://www.luogu.com.cn/problem/P1352
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <deque>
#include <vector>
using namespace std;
using ll = long long;
const int inf = 0x3f3f3f3f;
#define rf(i, n) for (int i = 1; i <= (n); ++i)
int rad();  // read int
const int max_size = 5 + 100;
const int maxn = 5 + 6000;

int n;
int a[maxn];

int root;
vector<int> tree[maxn];
int in_deg[maxn];

int f[maxn][2];

// 记忆化搜索
// int dfs(int x, bool come) {
//     if (f[x][come]) return f[x][come];
//     if (come) {
//         f[x][come] += a[x];
//         for (auto &p : tree[x])
//             f[x][come] += dfs(p, 0);
//     } else {
//         for (auto &p : tree[x])
//             f[x][come] += max(dfs(p, 0), dfs(p, 1));
//     }
//     return f[x][come];
// }

// 后序遍历 dp
// void dfs(int now) {
//     for (auto &p : tree[now])
//         dfs(p);
//     f[now][1] += a[now];
//     for (auto &p : tree[now])
//         f[now][1] += f[p][0], f[now][0] += max(f[p][1], f[p][0]);
// }
deque<int> order;

int main() {
    n = rad();
    rf(i, n) a[i] = rad();
    rf(i, n - 1) {
        int l = rad(), r = rad();
        tree[r].push_back(l);
        in_deg[l]++;
    }
    rf(i, n) if (in_deg[i] == 0) {
        root = i;
        break;
    }

    // printf("%d", max(dfs(root, 0), dfs(root, 1)));
    // dfs(root);
    // printf("%d", max(f[root][0], f[root][1]));

    // 逆拓扑序 dp
    order.push_back(root);
    for (int now = 0; now < n; ++now) {
        for (auto &p : tree[order[now]])
            if (--in_deg[p] == 0)
                order.push_back(p);
    }

    for (auto pos = order.rbegin(), finish = order.rend(); pos != finish; ++pos) {
        int now = *pos;
        f[now][1] += a[now];
        for (auto &p : tree[now])
            f[now][1] += f[p][0], f[now][0] += max(f[p][1], f[p][0]);
    }
    printf("%d", max(f[root][0], f[root][1]));
}

int rad() {
    int back = 0, ch = 0, neg = -1;
    while (ch != '-' && (ch < '0' || ch > '9'))
        ch = getchar();
    for (ch != '-' ? neg = 0 : ch = getchar(); ch >= '0' && ch <= '9'; ch = getchar())
        back = (back << 1) + (back << 3) + (ch & 0xf);
    return (back ^ neg) + (neg & 1);
}